/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.composedui.model;

import java.util.Vector;

public class DataItem {
    // Members
    private String name;
    private String imageFileName;
    private Vector dataItemListeners;

    public DataItem() {
        this("New DataItem", "/avatar-male.png");
    }

    public DataItem(String name, String imageFileName) {
        this.name = name;
        this.imageFileName = imageFileName;
        this.dataItemListeners = new Vector();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        setName(name, true);
    }

    public void setName(String name, boolean notify) {
        this.name = name;
        
        if (notify) {
            notifyDataItemListeners();
        }
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        setImageFileName(imageFileName, true);
    }

    public void setImageFileName(String imageFileName, boolean notify) {
        this.imageFileName = imageFileName;
        if (notify) {
            notifyDataItemListeners();
        }
    }

    // Observable design pattern implementation

    public void addDataItemListener(DataItemListener listener) {
        if (dataItemListeners.contains(listener) == false) {
            dataItemListeners.addElement(listener);
            listener.itemDataChanged(this);
        }
    }

    public void removeDataItemListener(DataItemListener listener) {
        if (dataItemListeners.contains(listener)) {
            dataItemListeners.removeElement(listener);
        }
    }

    private void notifyDataItemListeners() {
        for (int i = 0; i < dataItemListeners.size(); i++) {
            ((DataItemListener) dataItemListeners.elementAt(i))
                    .itemDataChanged(this);
        }
    }
}